package vip.xiaonuo.wydemop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Playwright 演示控制器
 * 提供 REST API 来触发 Playwright 演示
 */
@RestController
@RequestMapping("/playwright")
public class PlaywrightController {

    @Autowired
    private PlaywrightDemo playwrightDemo;

    /**
     * 基础演示
     * GET /playwright/basic
     */
    @GetMapping("/basic")
    public String basicDemo() {
        try {
            playwrightDemo.basicExample();
            return "基础演示执行完成！检查控制台输出和浏览器窗口。";
        } catch (Exception e) {
            return "演示执行失败: " + e.getMessage();
        }
    }

    /**
     * 进阶演示
     * GET /playwright/advanced  
     */
    @GetMapping("/advanced")
    public String advancedDemo() {
        try {
            playwrightDemo.advancedExample();
            return "进阶演示执行完成！检查控制台输出、浏览器窗口和截图文件。";
        } catch (Exception e) {
            return "进阶演示执行失败: " + e.getMessage();
        }
    }

    /**
     * 手机模式演示
     * GET /playwright/mobile
     */
    @GetMapping("/mobile")
    public String mobileDemo() {
        try {
            playwrightDemo.mobileExample();
            return "手机模式演示执行完成！检查控制台输出和手机版截图。";
        } catch (Exception e) {
            return "手机模式演示执行失败: " + e.getMessage();
        }
    }

    /**
     * 二维码登录演示
     * GET /playwright/qr-login
     */
    @GetMapping("/qr-login")
    public String qrLogin() {
        try {
            playwrightDemo.qrLoginExample();
            return "二维码登录演示完成！请查看控制台的二维码并扫描登录。";
        } catch (Exception e) {
            return "二维码登录演示失败: " + e.getMessage();
        }
    }

    /**
     * 检查登录状态
     * GET /playwright/login-status
     */
    @GetMapping("/login-status")
    public String checkLoginStatus() {
        try {
            boolean isLoggedIn = playwrightDemo.checkLoginStatus();
            return isLoggedIn ? "用户已登录" : "用户未登录";
        } catch (Exception e) {
            return "登录状态检查失败: " + e.getMessage();
        }
    }

    /**
     * API 说明
     * GET /playwright/help
     */
    @GetMapping("/help")
    public String help() {
        return """
                Playwright Java 演示 API:
                
                GET /playwright/basic       - 基础演示：打开抖音创作者平台
                GET /playwright/advanced    - 进阶演示：页面交互和截图
                GET /playwright/mobile      - 手机模式演示：模拟手机访问
                GET /playwright/qr-login    - 二维码登录：获取并展示二维码供扫描登录
                GET /playwright/login-status - 检查当前登录状态
                GET /playwright/help        - 显示此帮助信息
                
                注意：
                1. 首次运行需要下载浏览器文件，可能需要几分钟
                2. 演示会打开真实的浏览器窗口
                3. 截图文件保存在项目根目录
                4. 二维码登录会在控制台显示二维码，请用抖音APP扫描
                5. 登录状态会自动保存到playwright-user-data目录，下次访问会保持登录状态
                """;
    }
}